package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallStorage;
import com.tencreat.domain.mall.MallStorageType;
import com.tencreat.service.mall.IMallStorageService;
import com.tencreat.service.mall.IMallStorageTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 文件存储Controller
 *
 * @author tencreat
 * @date 2021-08-15
 */
@Api(tags="文件存储api")
@RestController
@RequestMapping("/mall/storage")
public class AdminMallStorageController extends BaseController {
    @Autowired
    private IMallStorageService mallStorageService;
    @Autowired
    private IMallStorageTypeService mallStorageTypeService;

    /**
     * 查询文件存储列表
     */
    @ApiOperation(value="查询文件存储列表")
    @PostMapping("/list")
    public HttpResult<IPage<MallStorage>> list(@RequestBody  MallStorage mallStorage) {
        QueryWrapper<MallStorage> wrapper = Wrappers.query(mallStorage);
        Page<MallStorage> page =  new Page<>(mallStorage.getPageNum(), mallStorage.getPageSize());
        return HttpResult.ok(mallStorageService.page(page, wrapper));
    }

    /**
     * 查询文件存储类型列表
     */
    @ApiOperation(value="查询文件存储类型列表")
    @GetMapping("/typeList")
    public HttpResult<List<MallStorageType>> typeList() {
       return HttpResult.ok(mallStorageTypeService.lambdaQuery().list());
    }

    /**
     * 新增文件存储类型
     */
    @ApiOperation(value="新增文件存储类型")
    @PreAuthorize("@ss.hasPermi('mall:storage:add')")
    @Log(title = "文件存储", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addType")
    public HttpResult<Boolean> addType(@RequestBody  MallStorageType mallStorage) {
        mallStorage.setCreateTime(LocalDateTime.now());

        return to(mallStorageTypeService.save(mallStorage));
    }

    /**
     * 修改文件存储类型
     */
    @ApiOperation(value="修改文件存储类型")
    @PreAuthorize("@ss.hasPermi('mall:storage:updata')")
    @Log(title = "文件存储", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateType")
    public HttpResult<Boolean> updateType(@RequestBody  MallStorageType mallStorage) {
        mallStorage.setUpdateTime(LocalDateTime.now());

        return to(mallStorageTypeService.updateById(mallStorage));
    }

    /**
     * 删除文件存储类型
     */
    @ApiOperation(value="删除文件存储类型")
    @PreAuthorize("@ss.hasPermi('mall:storage:remove')")
    @Log(title = "文件存储", businessType = BusinessType.DELETE)
    @PostMapping("/removeType")
    public HttpResult<Boolean> removeType(@RequestBody  ReqIdsParam ids) {
        // 查询是否有文件
        Integer count = mallStorageService.lambdaQuery().eq(MallStorage::getTypeId, ids.getId().get(0)).count();
        if (count != null && count >0){
            return HttpResult.error("请先删除该类型下面的所有图片");
        }

        return to(mallStorageTypeService.removeByIds(ids.getId()));
    }

    /**
     * 导出文件存储列表
     */
    @ApiOperation(value="导出文件存储列表")
    @PreAuthorize("@ss.hasPermi('mall:storage:export')")
    @Log(title = "文件存储", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallStorage mallStorage) {
        List<MallStorage> list = mallStorageService.selectMallStorageList(mallStorage);
        ExcelUtil<MallStorage> util = new ExcelUtil<MallStorage>(MallStorage.class);
        return util.exportExcel(list, "storage");
    }

    /**
     * 获取文件存储详细信息
     */
    @ApiOperation(value="获取文件存储详细信息")
    @PreAuthorize("@ss.hasPermi('mall:storage:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallStorage> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallStorageService.selectMallStorageById(id));
    }

    /**
     * 新增文件存储
     */
    @ApiOperation(value="新增文件存储")
    @PreAuthorize("@ss.hasPermi('mall:storage:add')")
    @Log(title = "文件存储", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody  MallStorage mallStorage) {
        return to(mallStorageService.insertMallStorage(mallStorage));
    }

    /**
     * 修改文件存储
     */
    @ApiOperation(value="修改文件存储")
    @PreAuthorize("@ss.hasPermi('mall:storage:edit')")
    @Log(title = "文件存储", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody  MallStorage mallStorage) {
        return to(mallStorageService.updateMallStorage(mallStorage));
    }

    /**
     * 删除文件存储
     */
    @ApiOperation(value="删除文件存储")
    @PreAuthorize("@ss.hasPermi('mall:storage:remove')")
    @Log(title = "文件存储", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(mallStorageService.deleteMallStorageByIds(ids));
    }
}
